#Working directory to file directory

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))


#Install Required Packages


packages <- c("tidyverse", "stringr")

# Check if each package is installed, install if missing, and load it
for (pkg in packages) {
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg)
    require(pkg, character.only = TRUE)
  }
}

rawdata2 <- read.csv("study2data_unlabelled.csv")

#Removing true independents and week with faulty randomization
rawdata2 = rawdata2 |> dplyr::filter(!pid7 == 4) |> filter(!survey_week == 86) |> filter(!survey_week == 89)


data2 = rawdata2 |> dplyr::select( starts_with("compliance") |
                           ends_with("_choice") | ends_with("party_ord")  | 
                           ends_with("endtext_rand") | starts_with("Check") | 
                           caseid | office | pid7 | democrat_therm_1 | 
                           republican_therm_1 | vote_importance | 
                           presvote20post | starts_with("norm_") |
                             gender | race | birthyr)

data2 = data2 |> dplyr::select(!ends_with("pipe"))


data2 = pivot_longer(data2, cols = ends_with("choice"), names_to = "condition",
                    values_to = "choice")

#33.6 got manipulation check right


data2 = data2 |> mutate(violType = case_when(startsWith(condition, "Baseline") ~ compliance_baseline,
                                         startsWith(condition, "Vignette1") ~ compliance1,
                                         startsWith(condition, "Vignette2") ~ compliance2))
data2 = data2 |> mutate(priming = case_when(startsWith(condition, "Baseline") ~ baseline_endtext_rand,
                                             startsWith(condition, "Vignette1") ~ block1_endtext_rand,
                                             startsWith(condition, "Vignette2") ~ block2_endtext_rand))
data2 = data2 |> mutate(violation = case_when(condition == "Baseline_choice" ~ 0,
                                              condition == "Vignette1_choice" ~ 1,
                                              condition == "Vignette2_choice" ~ 1))
data2 = data2 |> mutate(choice = choice - 1)
data2 = data2 |> mutate(affpol = abs(democrat_therm_1 - republican_therm_1))

data2 = data2 |> select(Check_flag, caseid, office, pid7, affpol,
                        vote_importance, presvote20post, norm_judges, norm_censorship,
                        norm_loyalty, norm_polling, norm_companies,
                        condition, choice, violType, priming, violation, Check,
                        gender, race, birthyr)

data2 = data2 |> mutate(officeF = case_when(office == 1 ~ "Local",
                                            office == 2 ~ "State",
                                            office == 3 ~ "Federal"))

data2 = data2 |> mutate(normF = case_when(violType == 1 ~ "Electoral Integrity",
                                          violType == 2 ~ "Judicial Integrity",
                                          violType == 3 ~ "Free Speech",
                                          violType == 4 ~ "Electoral Fairness",
                                          violType == 5 ~ "Division of Powers"))

data2 = data2 |> mutate(normSupp = case_when(normF == "Electoral Integrity"~norm_polling,
                                             normF == "Judicial Integrity"~norm_judges,
                                             normF == "Free Speech"~norm_censorship,
                                             normF == "Electoral Fairness"~norm_polling,
                                             normF == "Division of Powers"~norm_loyalty))

data2 = data2 |> mutate(CheckD = case_when(Check == 1 ~ "All candidates were \nrunning for (office)",
                                           Check == 2 ~ "Some candidates were not affiliated \nwith a political party",
                                           Check == 3 ~ "The prominent issue in most of \nthe elections was education",
                                           Check == 4 ~ "Candidates shared same opinions \non many public policies"))

data2 = data2 |> mutate(Check_flag = as.character(recode(Check_flag, "1" = "Pass", "2" = "Fail")))